import { request } from '../utils/request.js'
import { setToken, getToken } from '../utils/auth.js'
import store from '../store'
import { Toast } from '../utils/tools.js'
import { uploadURL, baseURL } from '../common/config.development'

//上传
export async function Upload(file) {
	try {
		console.log(file)
		return new Promise((resolve, reject) => {
			uni.uploadFile({
				url: '/Ctrls/Upload',
				filePath: file,
				name: 'file',
				success({ data }) {
					console.log(data)
				},
				fail: (err) => {
					reject(err);
				}
			})
		})
	} catch (e) {
		return {}
	}
}

//上传头像/照片
export async function UploadImageFile(filePath) {
	try {
		return new Promise((resolve, reject) => {
			uni.showLoading()
			uni.uploadFile({
				url: baseURL + '/Ctrls/Upload',
				filePath: filePath,
				header: {
					'LoginToken': getToken()
				},
				name: 'filename',
				success(res) {
					console.log(res)
					if (res.statusCode == 200) {
						const { data, errorMessage, excuteResult } = JSON.parse(res.data)
						if (excuteResult != 0) {
							Toast(errorMessage)
						}
						resolve({
							success: excuteResult == 0,
							errorMessage: errorMessage,
							data: data[0],
						});
					} else {
						reject(false);
					}
				},
				fail: (e) => {
					reject(e);
				},
				complete() {
					uni.hideLoading()
				},
			})
		})
	} catch (e) {
		return {}
	}
}



//图片上传
export async function UploadFileFromBase64(data) {
	try {
		let res = await request({
			url: "/ExcuteUploadFile/UploadFileFromBase64",
			method: "post",
			data
		});
		return res;
	} catch (e) {
		console.log(e);
		return false;
	}
}





// 获取验证码
export async function SendCode(data) {
	try {
		let res = await request({
			url: '/Public/GetVerificationCodeByPost',
			method: "post",
			data
		})
		if (res.excuteResult === 0) {
			Toast("发送成功")
		} else {
			Toast(res.errorMessage);
			console.log(res);
		}
		return res
	} catch (e) {
		return []
	}
}
